<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Minime_Pubsubhubbub_Subscribers {
    
    private $subscribers = array();
    
    const ELEMENT_ROOT = "pubsubhubbub_subscribers";
    
    public function __construct() {
        
    }

    public function addSubscriber(Minime_Pubsubhubbub_Subscriber  $sub) {
        $this->subscribers[$sub->getSubID()] = $sub;
    }
    
    public function getSubscriber($subID) {
        if(isset($this->subscribers[$subID])) {
            return $this->subscribers[$subID];
        }
        return null;
    }
    
    public function removeSubscriber($subID) {
        unset($this->subscribers[$subID]);
    }
    
    public function getSubscribers() {
        return $this->subscribers;
    }
    
    public function asXml() {
        
        $doc = new DOMDocument('1.0', 'UTF-8');
        // we want a nice output
        $doc->formatOutput = true;

        $root = $doc->createElement(self::ELEMENT_ROOT);
        $root = $doc->appendChild($root);
        
        foreach($this->subscribers as $subscriber) {
            $root->appendChild($subscriber->asDOMElement($doc));
        }
        
        return $doc->saveXML();
    }
   
    /**
     * We should have input something similar to this in the method:
     * 
	 * <pubsubhubbub_subscribers>
  	 *   <pubsubhubbub_subscriber id="a9a1819b0ab148eff45100d3bbfdbbd6">
  	 *     <callback>http://callback.com/callback.php</callback>
  	 *     <topic>http://identi.ca/api/statuses/user_timeline/209172.atom</topic>
  	 *     <lease_seconds>60</lease_seconds>
  	 *     <secret>SECRETQSDF345SDRT35ERG35SDFG35SRG</secret>
  	 *     <verify_token>4887bdf3f67a025e2b0a622b7f19de85</verify_token>
  	 *     <title>This is the topic of the followed node.</title>
  	 *     <subid>a9a1819b0ab148eff45100d3bbfdbbd6</subid>
  	 *   </pubsubhubbub_subscriber>
  	 *   ...
	 * <pubsubhubbub_subscribers>
	 * @return Minime_Pubsubhubbub_Subscribers 
	 */
    public static function buildFromRawXML($rawXML) {
        $dom = new DOMDocument();
        if(@$dom->loadXML($rawXML)) {
            return self::buildFromDom($dom);
        }
        throw new Exception("Invalid raw XML data provided!");
	}
	
	public static function buildFromDom(DOMDocument $dom) {
		
	    $subscribers = new Minime_Pubsubhubbub_Subscribers();
		
	    $subscribers_elm = $dom->getElementsByTagName(self::ELEMENT_ROOT)->item(0);
	    if(!$subscribers_elm) {
	        throw new Exception("Invalid XML! No pubsubhubbub_subscribers root element found!");
	    }
	    
	    foreach ($subscribers_elm->getElementsByTagName('pubsubhubbub_subscriber') as $subscriber_elm) {
	        $subscribers->addSubscriber(Minime_Pubsubhubbub_Subscriber::buildFromDOMElement($subscriber_elm));
	    }
	    
	    return $subscribers;
	}
}
